package com.ffcs.cn.action.map.drawing;

import com.supermap.data.DatasetType;
import com.supermap.desktop.core.Application;
import com.supermap.desktop.core.Interface.IBaseItem;
import com.supermap.desktop.core.Interface.IFormMap;
import com.supermap.desktop.core.implement.CtrlAction;
import com.supermap.mapping.Layer;
import com.supermap.ui.Action;
import com.supermap.ui.MapControl;

import java.awt.*;

/**
 * 绘制矩形
 *
  */
public class CtrlActionDrawingRect extends CtrlAction {

	public CtrlActionDrawingRect(IBaseItem caller) {
		super(caller);
	}

	@Override
	protected void run() {
		IFormMap formMap = (IFormMap) Application.getActiveApplication().getActiveForm();
		MapControl mapControl = formMap.getMapControl();
		// 获取当前可编辑的图层
		Layer layer = formMap.getMapControl().getActiveEditableLayer();
		// 图层可编辑且数据集类型为线时才可以添加
		if (layer != null && layer.getDataset().getType() == DatasetType.REGION) {
			mapControl.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
			mapControl.setAction(Action.CREATERECTANGLE);
		}
	}

	@Override
	public boolean enable() {
		boolean isEnable = false;
		if (Application.getActiveApplication().getActiveForm() instanceof IFormMap) {
			IFormMap formMap = (IFormMap) Application.getActiveApplication().getActiveForm();
			Layer layer = formMap.getMapControl().getActiveEditableLayer();
			if (layer != null && layer.getDataset().getType() == DatasetType.REGION) {
				isEnable = true;
			}
		}
		return isEnable;
	}
}
